/* Includes ------------------------------------------------------------------*/
#include "syscfg.h"
#include "stdio.h"
#include "string.h"
#include "puresun_func.h"
#include "jsmn_parser.h"

/* Private macro -------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
cfg_err_t cfg_read_string(string key, string value_out, string default_value, int maxlen);

/* Private functions ---------------------------------------------------------*/

/******************************************************************************
 * Function Name  : syscfg_get_work_mode
 * Description    : get work mode from syscfg file
 * Input          : None
 * Output         : None
 * Return         : work_mode_t
 *******************************************************************************/
work_mode_t syscfg_get_work_mode(void)
{
    work_mode_t mode = WORK_MODE_MANUAL;
    char mode_str[16] = {0};
    cfg_err_t cfg_err = cfg_read_string("user.work_mode", (string)mode_str, "manual", 16);
    if (cfg_err == CFG_ERR_OK)
    {
        if (strcmp(mode_str, "auto") == 0)
        {
            mode = WORK_MODE_AUTO;
        }
        else if (strcmp(mode_str, "manual") == 0)
        {
            mode = WORK_MODE_MANUAL;
        }
        else if (strcmp(mode_str, "debug") == 0)
        {
            mode = WORK_MODE_DEBUG;
        }
        else if (strcmp(mode_str, "setting") == 0)
        {
            mode = WORK_MODE_SETTING;
        }
        else
        {
            mode = WORK_MODE_MANUAL;
        }
    }
    return mode;
}

/******************************************************************************
 * Function Name  : syscfg_set_work_mode
 * Description    : set work mode to syscfg file
 * Input          : [work_mode_t mode]
 * Output         : None
 * Return         : None
 *******************************************************************************/
void syscfg_set_work_mode(work_mode_t mode)
{
    return;
}

/******************************************************************************
 * Function Name  : cfg_read_string
 * Description    : read string from syscfg file
 * Input          : [string key, string value_out, string default_value, int maxlen]
 * Output         : None
 * Return         :
 *******************************************************************************/
cfg_err_t cfg_read_string(string key, string value_out, string default_value, int maxlen)
{
    if (pf_file_exist(SYS_CFG_FILE_PATH) == false)
    {
        return CFG_ERR_FILE_NOT_EXIST;
    }

    // read data from syscfg file
    js_vessel_t js_vessel;
    json_err_t js_err = read_json_file(SYS_CFG_FILE_PATH, (byte **)&js_vessel.json, true);
    if (js_err != JSON_OK)
    {
        return CFG_ERR_READ_FILE;
    }

    // parse json data
    js_err = parse_json_buffer(&js_vessel);
    if (js_err != JSON_OK)
    {
        return CFG_ERR_PARSE_FILE;
    }

    // get value from json data
    js_err = get_string_by_path(&js_vessel, key, value_out, default_value, maxlen);
    if (js_err != JSON_OK)
    {
        free_json_vessel(&js_vessel);
        return CFG_ERR_READ_KEY_FAIL;
    }
    free_json_vessel(&js_vessel);
    return CFG_ERR_OK;
}

/******************************************************************************
 * Function Name  : cfg_save
 * Description    : save syscfg to file
 * Input          : [syscfg_t *syscfg]
 * Output         : None
 * Return         : None
 *******************************************************************************/
void save_syscfg(syscfg_t *syscfg)
{
    FILE *fp = fopen(SYS_CFG_FILE_PATH, "w");
    if (fp == NULL)
    {
        return;
    }

    fprintf(fp, "{");
    fprintf(fp, "\"user\":{");
    if (syscfg->user.work_mode == WORK_MODE_AUTO)
    {
        fprintf(fp, "\"work_mode\":\"auto\"");
    }
    else if (syscfg->user.work_mode == WORK_MODE_MANUAL)
    {
        fprintf(fp, "\"work_mode\":\"manual\"");
    }
    else if (syscfg->user.work_mode == WORK_MODE_DEBUG)
    {
        fprintf(fp, "\"work_mode\":\"debug\"");
    }
    else if (syscfg->user.work_mode == WORK_MODE_SETTING)
    {
        fprintf(fp, "\"work_mode\":\"setting\"");
    }
    else
    {
        fprintf(fp, "\"work_mode\":\"manual\"");
    }
    fprintf(fp, "},");
    fprintf(fp, "\"system\":{");
    if (syscfg->system.wifi_sta == true)
    {
        fprintf(fp, "\"wifi_sta\":\"true\"");
    }
    else
    {
        fprintf(fp, "\"wifi_sta\":\"false\"");
    }
    fprintf(fp, "}");
    fprintf(fp, "}");
    fclose(fp);
}

/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/
